package com.migo.api;

import com.alibaba.fastjson.JSONObject;
import com.migo.annotation.IgnoreAuth;
import com.migo.entity.AppDownloadEntity;
import com.migo.entity.MgSampleVideoEntity;
import com.migo.entity.TeachingVideoEntity;
import com.migo.entity.TechnicalSupportTypeEntity;
import com.migo.service.TeachingVideoService;
import com.migo.utils.JsonUtils;
import com.migo.utils.R;
import com.migo.utils.redis.RedisCacheUtil;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Comparator;
import java.util.List;
import java.util.Map;

/**
 * Created by Wes on 2019/1/16.
 */
@CrossOrigin
@RestController
@RequestMapping("/api")
public class ApiTeachingVideoController extends ApiBaseController {


    @Autowired
    private TeachingVideoService teachingVideoService;


    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private RedisCacheUtil<List<TeachingVideoEntity>> redisCacheUtil;

    /**
     * 查询产品的教学视频
     */
    @IgnoreAuth
    @RequestMapping("/queryTeachingVideoList")
    public R queryTeachingVideoList(String proId, String language) throws Exception {
        Map<String, Object> paramMap = new HashedMap();
        String result = "";
        List<TeachingVideoEntity> teachingVideoEntities = null;
        try {
            paramMap.put("proId", proId);
            paramMap.put("language", language);
            //缓存处理开始
            String key = "queryTeachingVideoList_" + proId + "_" + language;
            if (redisCacheUtil.hashKey(key)) {
                teachingVideoEntities = redisCacheUtil.getCacheList(key);
                teachingVideoEntities.sort(Comparator.comparing(TeachingVideoEntity::getId).reversed());
            } else {
                //查询数据库写入redis
                synchronized (this) {
                    teachingVideoEntities = teachingVideoService.queryList(paramMap);
                    redisCacheUtil.setCacheList(key, teachingVideoEntities);
                }
            }
            //缓存部分 结束
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.ok().put("data", teachingVideoEntities);
    }


    /**
     * 查询产品的教学视频
     */
    @IgnoreAuth
    @RequestMapping("/queryTeachingVideoListForRedis")
    public R queryTeachingVideoListForRedis(String proId, String language) throws Exception {
        Map<String, Object> paramMap = new HashedMap();
        String result = "";
        try {
            paramMap.put("proId", proId);
            paramMap.put("language", language);

            //缓存处理开始
            String key = "queryTeachingVideoList_" + proId + "_" + language;
            boolean exisKey = redisTemplate.hasKey(key);
            if (exisKey) {
                result = redisTemplate.boundValueOps(key).get();
                if (result == null || result.isEmpty()) {
                    result = JsonUtils.getJSONString(teachingVideoService.queryList(paramMap));
                    redisTemplate.boundValueOps(key).set(result);
                }
            } else {
                result = JsonUtils.getJSONString(teachingVideoService.queryList(paramMap));
                redisTemplate.boundValueOps(key).set(result);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        List<TeachingVideoEntity> newList = JSONObject.parseArray(result, TeachingVideoEntity.class);
        return R.ok().put("data", newList);
    }

    /**
     * 查询教学视频详情
     */
    @IgnoreAuth
    @RequestMapping("/queryTeachingVideoDetail")
    public R queryTeachingVideoDetail(String id, String language) throws Exception {
        String result = "";
        //缓存处理开始
        String key = "queryTeachingVideoDetail_" + id + "_" + language;
        boolean exisKey = redisTemplate.hasKey(key);
        if (exisKey) {
            result = redisTemplate.boundValueOps(key).get();
            if (result == null || result.isEmpty()) {
                result = JsonUtils.getJSONString(teachingVideoService.queryObject(Integer.parseInt(id)));
                redisTemplate.boundValueOps(key).set(result);
            }
        } else {
            result = JsonUtils.getJSONString(teachingVideoService.queryObject(Integer.parseInt(id)));
            redisTemplate.boundValueOps(key).set(result);
        }
        TeachingVideoEntity teachingVideoEntity = JSONObject.parseObject(result, TeachingVideoEntity.class);
        return R.ok().put("data", teachingVideoEntity);
    }


}
